/**
 * Created by liulinqiang on 2018/7/3.
 */

import React from "react"
import axios from "axios"
import PropTypes from "proptypes"
//import '../data/user'
import { message,Form, Icon, Input, Button, Checkbox} from 'antd';
import { isAbsolute } from "path";
import {Provider,connect} from 'react-redux'
import store from '../../redux/store'
import RequestComponent from '../request/request'
import md5 from 'js-md5';
import sha256 from 'crypto-js/sha256';
require('../../styles/login.scss')

const FormItem = Form.Item;
class NormalLoginForm extends React.Component {
    constructor(){
        super();
    }
    componentWillMount() {

    }
    handleSubmit(e){
      // let rq=new RequestComponent('','','');
      // console.log(process.env)
       RequestComponent.initHost('local')
        e.preventDefault();
        let data = this.props.form.getFieldsValue()
        const {name,password}=data;
      data.password=sha256(password).toString()
      console.log('sha256 pwd: ',data.password)
      data.orgCode='HN'
      data.pictureCodeResult='HN'
      data.pictureCodeUUID='HN'
      data.rememberMe='false'

        let history = this.context.router.history
      RequestComponent.promissionAxios.post('/open-api/permission/login',data).then((res) =>{

        console.log('res',res)
        let data_ = res.data;
            if(data_.code===200){
              let userInfo=data_.data.userInfo;
              let token=data_.data.token;
                new RequestComponent(userInfo.id,token,'org');
                localStorage.setItem('basicUrl',RequestComponent.httpUrl)
                localStorage.setItem('headers',RequestComponent.header)
                store.dispatch({type:'login',username:data.name})
                history.push('index');
            }else{
                message.error('用户或密码不正确')
            }
        })
      //1. 放到公共组件静态属性里面去
      let req =new RequestComponent('1008611','token-afab9ba809ba9','sys');
      //2. 放到cookie 里面去供单点登陆使用
      document.cookie="header="+JSON.stringify(RequestComponent.header)

      //set domain(.xxxxx);
    }
    render() {
        let top = window.innerHeight/2;
        let left = window.innerWidth/2;
        let message = this.message
        const {getFieldDecorator} = this.props.form;
        return (
            <div className="login-wrap">
                <Form onSubmit={(e)=>this.handleSubmit(e)} className="loginForm">
                    <h5>用户登陆</h5>
                    <FormItem>
                        {
                            getFieldDecorator(
                                'account',{
                                    rules:[{required: true, message:'用户名不能为空！'}],
                                    initialValue: 'yishaling'
                                }
                            )(
                                <Input size="large"  prefix={<Icon type="user"  style={{ color: 'rgba(0,0,0,.5)' }}  />} placeholder="请输入用户名"  />
                            )
                        }
                    </FormItem>
                    <FormItem>
                        {
                            getFieldDecorator(
                                'password',{
                                    rules:[{required: true, message:'密码不能为空！',trigger:'blur'}],
                                    initialValue: 'r08ml8&pK%D3aGEt'
                                }
                            )(
                                <Input size="large"  prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.5)' }} />} type="password" placeholder="请输入密码" />
                            )
                        }
                    </FormItem>
                    <FormItem>
                    <Button size="large"  type="primary" htmlType="submit" className="login-form-button" >
                            登陆
                        </Button>
                    </FormItem>
                    <p>鉴权系统</p>
                    <p>By YSL</p>
                </Form>
            </div>
        );
    }
}

NormalLoginForm.contextTypes = {
    router: PropTypes.object.isRequired
};
let WrapForm = Form.create()(NormalLoginForm)

// const WrapForms  = connect()(WrapForm)
export default WrapForm
